import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_base/generated/l10n.dart';
import 'package:flutter_base/src/config/app_theme.dart';
import 'package:flutter_base/src/config/color_util.dart';
import 'package:flutter_base/src/provider/locale_provider.dart';
import 'package:flutter_base/src/provider/theme_provider.dart';
import 'package:flutter_base/src/routes/app_routes.dart';
import 'package:provider/provider.dart';
// ignore: depend_on_referenced_packages
import 'package:flutter_localizations/flutter_localizations.dart';

void main() {
  SystemChrome.setSystemUIOverlayStyle(
    const SystemUiOverlayStyle(
      // 导航栏的背景色
      systemNavigationBarColor: ColorUtils.whiteColor,
      // 导航栏上方那根 1dp 的分隔线颜色
      systemNavigationBarDividerColor: Colors.transparent,
      // 导航栏图标的亮度模式
      systemNavigationBarIconBrightness: Brightness.dark, // 黑色图标
      statusBarIconBrightness: Brightness.dark,
      // 状态栏的背景色
      statusBarColor: ColorUtils.backgroundColor,
    ),
  );
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (context) => LocaleProvider()),
        ChangeNotifierProvider(create: (context) => ThemeProvider()),
      ],
      child: const MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    final localeProvider = Provider.of<LocaleProvider>(context);
    return MaterialApp.router(
      debugShowCheckedModeBanner: false,
      theme: AppTheme.lightTheme,
      darkTheme: AppTheme.darkTheme,
      themeMode: Provider.of<ThemeProvider>(context).themeMode,
      routerConfig: AppRoutes.router,
      localizationsDelegates: const [
        AppL10n.delegate, // 添加AppL10n代理
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [
        Locale('zh', 'CN'), // 支持的语言
        Locale('en', 'US'),
      ],
      // locale: const Locale('zh', 'CN'), // 默认语言
      locale: localeProvider.locale,
      builder: (context, child) {
        return GestureDetector(
          onTap: () {
            /// 点击空白区域隐藏键盘
            FocusScope.of(context).unfocus();
            FocusManager.instance.primaryFocus?.unfocus();
          },
          child: child,
        );
      },
    );
  }
}
